<?php
class AdController extends BaseManageController{
	public function actionAdmin(){
		$this->pageTitle = '广告组管理 - ' . Yii::app()->name;
		$mod = new AdMondel();
		$ads = $mod->find();
		$now = time();
		if(!empty($ads)){
			//统计有效广告数
			foreach($ads as &$_ad){
				$available_count = 0;
				if(isset($_ad['items'])){
					$_ad['total'] = count($_ad['items']); //总数
					foreach ($_ad['items'] as $item){
						if(isset($item['start'])){
							if($now < $item['start']){
								continue;
							}
						}
						if(isset($item['end'])){
							if($now > $item['end']){
								continue;
							}
						}
						++$available_count;
					}
				}else{
					$_ad['total'] = 0;
				}
				$_ad['available_total'] = $available_count;
			}
		}
		$this->render('admin', array(
			'ads' => $ads
		));
	}
	
	public function actionEdit($id = null){
		if(Yii::app()->request->isPostRequest){
			$this->_save($id);
		}
		$this->pageTitle = '广告组编辑 - ' . Yii::app()->name;
		$mod = new AdMondel();
		$ad = null;
		if($id !== null){
			$ad = $mod->get_by_key($id);
		}
		if(empty($ad)){
			$ad = array(
				'_id' 	=> '',
				'name' 	=> '广告组名称'
			);
		}else{
			if(isset($ad['items'])){
				foreach ($ad['items'] as $index=>&$item){
					if(isset($item['start'])){
						$item['start'] = date('Y-m-d', $item['start']);
					}
					if(isset($item['end'])){
						$item['end'] = date('Y-m-d', $item['end']);
					}
				}
			}
		}
		$this->render('edit', array(
			'model' => $ad
		));
	}
	
	private function _save($id){
		$ad = $_POST['Ad'];

		$mod = new AdMondel();
		
		if(!empty($ad['code'])){
			//检查是否有重名的
			$condition = array();
			if(empty($ad['_id'])){
				//新增
				$condition['code'] = $ad['code'];
			}else{
				$condition = array(
					'code' => $ad['code'],
					'_id' => array('$ne'=>$ad['_id'])
				);
			}
			if($mod->exists_attrs($condition)){
				$this->ajax_error('广告代码：“' . $ad['code'] . '”已经存在！');
			}
		}
		
		if(isset($_POST['titles'])){
			$titles = $_POST['titles'];
			$urls = $_POST['urls'];
			$starts = $_POST['starts'];
			$ends = $_POST['ends'];
			$descs = $_POST['descs'];
			$targets = $_POST['targets'];
			$pictures = $_POST['pictures'];
			
			$path = IMAGE_PATH . 'ad/' . date('Y/m/d/');
			CommonHelper::ymkdirs(ROOT_PATH . $path);
			
			$ad['items'] = array();
			//处理图片
			foreach ($titles as $index=>$title){
				$p = $pictures[$index];
				$item = array(
					'title' 	=> $title,
					'url'		=> $urls[$index],
					'desc'		=> $descs[$index],
					'target' 	=> $targets[$index],
				);
				if(preg_match('/.*\/uploads\/temp\/(.*)/', $p, $matchs)){
					$img_name = $matchs[1];
					rename(ROOT_PATH . $p, ROOT_PATH . $path . $img_name);
					$p = $path . $img_name;
					$item['picture'] = $p;
				}else{
					if(!empty($p)){
						$item['picture'] = $p;
					}
				}
				$start_str = $starts[$index];
				$end_str = $ends[$index];
				if(preg_match('/^\d{4}\-\d{1,2}\-\d{1,2}$/', $start_str)){
					//如果是日期格式
					$item['start'] = strtotime($start_str);
				}
				if(preg_match('/^\d{4}\-\d{1,2}\-\d{1,2}$/', $end_str)){
					//如果是日期格式
					$item['end'] = strtotime($end_str) + 3600 * 24 - 1;
				}
				$ad['items'][] = $item;
			}
		}
		$id = null;
		if(empty($ad['_id'])){
			$id = CommonHelper::gen_uuid();
			$ad['_id'] = $id;
			$ad['site_id'] = SITE_ID;
			$mod->insert($ad);
		}else{
			$id = $ad['_id'];
			$mod->update_by_key($id, $ad);
		}
		$this->ajax_info('保存成功！', $id);
	}
	
	public function actionSearch(){
		$con = array(
			'site_id' => SITE_ID,
		);
		
		$cate_id = null;
		$key = null;
		if(isset($_REQUEST['cate'])){
			$cate_id = $_REQUEST['cate'];
			if(!empty($cate_id)){
				$con['cate'] = $cate_id;
			}
		}
		if(isset($_REQUEST['keyword'])){
			$key = $_REQUEST['keyword'];
			if(!empty($key)){
				$con['$or'] = array(
					array(
						'title' => array('$regex'=>$key),		
					),
					array(
						'code' => array('$regex'=>$key),
					)
				);
			}
		}
		$ads = AdMondel::model()->find($con, 'name,code,title', 0, 20);
		$this->ajax_info('true', $ads);
	}
}
?>